import django.http as http
import django.shortcuts as shortcuts
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.conf import settings
from django.conf import settings

import django_community.models, django_community.utils, django_community.forms

from django_utils.pagination import paginate_queryset
from django_utils import request_helpers
import django_community.decorators as django_community_decorators
import django_openidconsumer.util, django_openidconsumer.views

def openid_login(request, base_error = None):
    print getattr(request, 'openids', 'yo')
    if request.POST:
        openid = request.POST.get('openid_identifier', '')
        if openid.strip():
            return django_openidconsumer.views.begin(request,
                                                    openid,
                                                    redirect_to = reverse('openid-complete'),
                                                    next = request_helpers.get_redirect_path(request),
                                                    on_failure = openid_login,
                                                    template_name = 'community/openid_login.html')
        else:
            error = 'empty_id'
    else:
        error = base_error
    return shortcuts.render_to_response(
        'community/openid_login.html', 
        {'error':error}, 
        context_instance = RequestContext(request),
    )

def openid_complete(request):
    return django_openidconsumer.views.complete(request, on_failure = openid_login)

def openid_logout(request):
    django_community.utils.openid_logout()
    redirect_url = reverse('main')
    return http.HttpResponseRedirect(redirect_url)

def combined_logout(request):
    django_community.utils.handle_logout(request)
    django_community.utils.openid_logout(request)
    path = request_helpers.get_redirect_path(request)
    return http.HttpResponseRedirect(path)

def combined_login(request):
    if settings.OPENID_ENABLED:
        return http.HttpResponseRedirect(request_helpers.append_redirect_path(reverse('openid-login'),  request))
    else:
        return http.HttpResponseRedirect(request_helpers.append_redirect_path(reverse('community-login'),  request))
    
@django_community_decorators.anonymous_required
def combined_signup(request):
    return shortcuts.render_to_response('community/combined_signup.html',  {},  context_instance=RequestContext(request))
    
def user_required(request):
    return shortcuts.render_to_response('community/user_required.html',  {},  context_instance = RequestContext(request))
    
def anonymous_required(request):
    return shortcuts.render_to_response('community/anonymous_required.html',  {},  context_instance = RequestContext(request))

def login(request):
    """login"""
    
    from django.contrib.auth import login
    
    if request.POST:
        login_form = forms.LoginForm(request.POST)
        if login_form.is_valid():
            path = request_helpers.get_redirect_path(request)
            user = login_form.get_user()
            login(request, user)
            if path:
                return http.HttpResponseRedirect(path)
            else:
                return http.HttpResponseRedirect(reverse('main'))
    else:
        login_form = forms.LoginForm()
    return shortcuts.render_to_response(
            'community/login.html', 
             {'login_form':login_form}, 
            context_instance = RequestContext(request), 
    )

@django_community_decorators.anonymous_required
def signup(request):
    """signup"""
    
    if request.POST:
        signup_form = forms.SignupForm(request.POST,  request.FILES,  request)
        if signup_form.is_valid():
            path = request_helpers.get_redirect_path(request)
            user,  error = django_community.utils.handle_signup(request,  request.POST)
            if path:
                return http.HttpResponseRedirect(path)
            else:
                return http.HttpResponseRedirect(reverse('main'))
    else:
        signup_form = forms.SignupForm()
    return shortcuts.render_to_response(
            'community/signup.html', 
            {'signup_form':signup_form}, 
            context_instance = RequestContext(request),
            )

def public_profile(request,  user_id,  user_name):
    user = models.User.objects.get(id = user_id)
    context = django_community.utils.build_profile_information(user)
    context['user'] = user
    
    return shortcuts.render_to_response(
        'community/public_profile.html', 
        context, 
        context_instance = RequestContext(request),
    )

@django_community_decorators.user_required
def private_profile(request):
    user = django_community.utils.get_logged_user(request)
    context = django_community.utils.build_profile_information(user)
    context['user'] = user
    
    return shortcuts.render_to_response(
        'community/private_profile.html', 
        context, 
        context_instance = RequestContext(request),
    )
    
def edit_profile(request):
    user = django_community.utils.get_logged_user(request)
    EditProfileForm = forms.build_edit_profile_form(user)
    
    if request.POST:
        edit_profile_form = EditProfileForm(request.POST,  request.FILES,  request)
        if edit_profile_form.is_valid():
            django_community.utils.handle_edit_profile(user,  request.POST)
            return http.HttpResponseRedirect(reverse('community-profile',  args=[user.id,  user.display_name]))
    else:
        edit_profile_form = EditProfileForm()
    return shortcuts.render_to_response(
        'community/edit_profile.html', 
        {'user':user,  'edit_profile_form':edit_profile_form}, 
        context_instance = RequestContext(request),
    )

def browse_users(request):
    users = models.User.objects.all()
    page = request_helpers.get_page(request)
    
    current_page,  page_range = paginate_queryset(users,  10,  5,  page)
    
    return shortcuts.render_to_response(
        'community/browse_users.html', 
        {'current_page':current_page,  'page_range':page_range}, 
        context_instance = RequestContext(request),
    )
